package net.rock.eshop.utils;

import net.rock.eshop.entity.User;
import net.rock.eshop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AdminChecker {
    static final int ADMIN_PERMISSION = 1;
    @Autowired
    private UserService userService;

    @SuppressWarnings("BooleanMethodIsAlwaysInverted")
    public boolean checkAdmin(String sessionId) {
        if (sessionId == null || sessionId.isEmpty()) {
            return false;
        }
        User user = userService.fetchUserBySessionId(sessionId);
        return user != null && user.getPermission() >= ADMIN_PERMISSION;
    }
}
